package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.EducationSelectAdapter;
import com.sx.zongzhi.bean.EducationBean;
import com.sx.zongzhi.presenter.EducationPest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IEducationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 学历选择
 */
public class EducationActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
        ,IEducationView{

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private EducationSelectAdapter selectAdapter;
    private List<EducationBean> educationBeanList=new ArrayList<>();
    private EducationPest educationPest;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_recyclerview);
        setTitleName(R.string.education);
        ButterKnife.bind(this);
        initView();
        onRefresh();
    }

    private void initView() {
        educationPest=new EducationPest(this);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        selectAdapter = new EducationSelectAdapter(this,R.layout.item_event_type,educationBeanList);
        selectAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        recyclerView.setAdapter(selectAdapter);
        selectAdapter.getEducation(new EducationSelectAdapter.EducationCallback() {
            @Override
            public void getData(EducationBean.SchoolingsBean schoolingsBean) {
                Intent intent=new Intent();
                intent.putExtra("SchoolingsBean",schoolingsBean);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public void onRefresh() {
        educationPest.getEducationList();
    }


    @Override
    public void showError(String message) {
        if (swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }
        ToastUtils.showToast(this,message);
    }


    @Override
    public void getEducationList(List<EducationBean> beanList) {
        swipeRefreshLayout.setRefreshing(false);
        if (beanList!=null&&beanList.size()>0){
            educationBeanList.clear();
            educationBeanList.addAll(beanList);
            selectAdapter.notifyDataSetChanged();
        }
    }
}
